/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package org.nfcsigning;

import org.nfcsigning.certificate.CertificateChain;
import org.nfcsigning.record.SignatureField;
import org.nfcsigning.record.SignatureRecord;
import org.nfcsigning.record.URIRecord;

//#ifdef MyJSR257
//# import my.javax.microedition.contactless.ndef.NDEFRecordType;
//#else
import javax.microedition.contactless.ndef.NDEFRecordType;
//#endif


/**
 *
 * @author Markus Kilås
 */
public class Signature {
    
    private SignatureRecord signatureRecord;
    private NDEFRecordType signatureRecordType;
    private SignatureStatus status;

    Signature(SignatureRecord signatureRecord, SignatureStatus status) {
        this(signatureRecord, SignatureRecord.RECORD_TYPE, status);
    }

    Signature(SignatureRecord signatureRecord, NDEFRecordType signatureRecordType, SignatureStatus status) {
        this.signatureRecord = signatureRecord;
        this.signatureRecordType = signatureRecordType;
        this.status = status;
    }

    Signature(SignatureRecord signatureRecord, NDEFRecordType signatureRecordType) {
        this(signatureRecord, signatureRecordType, null);
    }

    Signature(SignatureRecord signatureRecord) {
        this(signatureRecord, SignatureRecord.RECORD_TYPE, null);
    }
    
    /**
     * The actual signature bytes.
     * To remove the bytes from the signature and instead reference the signature 
     * bytes by an URI for instance use <code>setURI(new URIRecord("http://someuri"))</code>.
     * @return The bytes to be included in the signature.
     * @see #setURI(com.ericsson.nfc.signing.record.URIRecord) 
     */
    public byte[] getBytes() {
        return signatureRecord.getSignatureField().getSignature();
    }

    public CertificateChain getCertificateChain() {
        return status.getCertificateChain();
    }

    public boolean isValid() {
        return status.isValid();
    }
    
    /**
     * By setting the URI the signature bytes will be removed from the Signature and 
     * later calls to <code>getBytes()</code> might return null.
     * @param uri The URI to include in the signature instead of the bytes.
     * @see #getBytes() 
     */
    public void setURI(URIRecord uri) {
        signatureRecord = new SignatureRecord(
                new SignatureField(signatureRecord.getSignatureField().getType(), uri), 
                                    signatureRecord.getCertificateField());
    }
    
    public SignatureStatus getStatus() {
        return status;
    }
    
    SignatureRecord getSignatureRecord() {
        return signatureRecord;
    }

    NDEFRecordType getSignatureRecordType() {
        return signatureRecordType;
    }

}
